package com.xilihui.xlh.business.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xilihui.xlh.R;


/**
 * 弹出分享Dialog
 */
public class ShareDialog implements OnClickListener {

    public static final int WEIXIN = 0X00;
    public static final int WEIXIN_CIRCLE = 0X01;
    public static final int QQ = 0X02;
    public static final int QZONE = 0x03;

    private Dialog dialog;
    private TextView tvWechat;
    private TextView tvWechatCircle;
    private TextView tvQQ;
    private TextView tvQZone;

    private DialogShareCallBack mCallBack;

    public ShareDialog(Context context) {
        dialog = new Dialog(context, R.style.Dialog);
        dialog.setContentView(R.layout.dialog_share);
        // Dialog在底部
        Window window = dialog.getWindow();
        if (window != null)
            window.setGravity(Gravity.BOTTOM);
        // Dialog与屏幕同宽
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = context.getResources().getDisplayMetrics().widthPixels; // 设置宽度
        dialog.getWindow().setAttributes(lp);

        tvWechat = (TextView) dialog.findViewById(R.id.tv_share_wx);
        tvWechat.setOnClickListener(this);
        tvWechatCircle = (TextView) dialog.findViewById(R.id.tv_share_wx_circle);
        tvWechatCircle.setOnClickListener(this);
        tvQQ = (TextView) dialog.findViewById(R.id.tv_share_qq);
        tvQZone = (TextView) dialog.findViewById(R.id.tv_share_qzone);
        tvQQ.setOnClickListener(this);
        tvQZone.setOnClickListener(this);
        dialog.findViewById(R.id.pop_share_cancel).setOnClickListener(this);
        dialog.findViewById(R.id.info_outter).setOnClickListener(this);

        dialog.setCanceledOnTouchOutside(true);
    }

    public void setCallBack(DialogShareCallBack mCallBack) {
        this.mCallBack = mCallBack;
    }

    public void setWechatVisible(boolean b) {
        if (b) {
            tvWechat.setVisibility(View.VISIBLE);
        } else {
            tvWechat.setVisibility(View.GONE);
        }
    }

    public void setWechatCircleVisible(boolean b) {
        if (b) {
            tvWechatCircle.setVisibility(View.VISIBLE);
        } else {
            tvWechatCircle.setVisibility(View.GONE);
        }
    }

    public void setQQVisible(boolean b) {
        if (b) {
            tvQQ.setVisibility(View.VISIBLE);
        } else {
            tvQQ.setVisibility(View.GONE);
        }
    }

    public void setQZoneVisible(boolean b) {
        if (b) {
            tvQZone.setVisibility(View.VISIBLE);
        } else {
            tvQZone.setVisibility(View.GONE);
        }
    }

    public void show() {
        dialog.show();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_share_wx:          //微信
                dialog.dismiss();
                if (mCallBack != null) {
                    mCallBack.onSelected(WEIXIN);
                }
                break;
            case R.id.tv_share_wx_circle:   //朋友圈
                dialog.dismiss();
                if (mCallBack != null) {
                    mCallBack.onSelected(WEIXIN_CIRCLE);
                }
                break;
            case R.id.tv_share_qq:          // QQ
                dialog.dismiss();
                if (mCallBack != null) {
                    mCallBack.onSelected(QQ);
                }
                break;
            case R.id.tv_share_qzone:       //QZone
                dialog.dismiss();
                if (mCallBack != null) {
                    mCallBack.onSelected(QZONE);
                }
                break;
            case R.id.info_outter:
            case R.id.pop_share_cancel:
                dialog.cancel();
                break;
            default:
                break;
        }
    }

    public interface DialogShareCallBack {
        void onSelected(int platform);
    }
}
